/**
 * Created by Administrator on 2017/6/28 0028.
 */
$('#pages-users-adduser-btn .btn-mint').click(function () {
    var username = $('#adduser-input-username').val().trim();
    var usernamehuman=$('#adduser-input-name').val().trim();
    var password=$('#adduser-password-input').val().trim();
    var email=$('#adduser-input-email').val().trim();
    var groupid = $('#adduser-groups-input').val().trim();
    var usertype = $('#pages-users-usertype input:radio:checked').val();
    var active =$('#pages-users-isactive input:radio:checked').val();
    var expirydata =$('#pages-users-expirydata input:radio:checked').val();
    $.ajax({
        url:'/users/userservice/',
        type:'POST',
        data:{
            'username':username,
            'usernameHuman':usernamehuman,
            'email':email,
            'password':password,
            'usertype':usertype,
            'usergroup':groupid,
            'active':active,
            'expiration':expirydata,
        },
        success:function (data) {
            window.location.href='/users/userservice/';
        }
    });
$('#pages-users-adduser-Modal').modal('hide');
});


// change user is active
$('.userIsActive').click(function () {
    var self = this;
    var userIsActive = $(self).parent().attr('userIsActive');
    var userid = $(self).parent().parent().attr('userID');
    var active = 0;
    if (userIsActive ==1){
        $(self).removeClass('label-danger').addClass('label-success');
        $(self).text('是');
        $(self).parent().attr('userIsActive',0);
    }else if(userIsActive ==0){
        $(self).removeClass('label-success').addClass('label-danger');
        $(self).text('否');
        $(self).parent().attr('userIsActive',1);
        var active =1;
    }
    $.ajax({
        url:'/users/userservice/',
        type:'POST',
        data:{'uid':userid,'flag':'chengeActive','active':active},
        success:function (data) {
            console.log(data);
        }

    });
});

// del user
$('#adduser-del-user-btn').click(function () {
    var useridlist = [];
    $('.pages-users-selected').each(function () {
        var self =this;
        var userid = $(self).parent().parent().attr('userID')
        if($(self).prop('checked')){
        useridlist.push(userid)
        }
});

    if(useridlist.length==0){
        alert('请选择要删除的用户！')
    }else {
        $.ajax({
        url:"/users/userservice/",
        type:'POST',
        data:{'useridlist':useridlist,'flag':'del'},
        success:function (data) {
        window.location.href='/users/userservice/';
        }
    })
    }
});

// cancel all
$('#adduser-cancel-user-btn').click(function () {
    $('.pages-users-selected').each(function () {
        var self = this;
        if ($(self).prop('checked')) {
            $(self).prop('checked', false);
        }
    });

});

// select all
$('#adduser-selectall-user-btn').click(function () {
    $('.pages-users-selected').each(function () {
        var self = this;
        if ($(self).prop('checked') == false) {
            $(self).prop('checked', true);
        }
    });

});



// 当模态框隐藏式，初始化
$('#pages-users-adduser-Modal').on('hidden.bs.modal', function () {
    var username = $('#adduser-input-username').val('');
    var usernamehuman=$('#adduser-input-name').val('');
    var password=$('#adduser-password-input').val('');
    var email=$('#adduser-input-email').val('');
    var groupid = $('#adduser-groups-input').val('');
});